package gold.digger;

import gold.utils.InputUtil;
import gold.utils.UF;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC947 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {

        public class UF {
            // 连通分量个数
            private int count;
            // 存储一棵树
            private int[] parent;
            // 记录树的“重量”
            private int[] size;

            public UF(int n) {
                this.count = n;
                parent = new int[n];
                size = new int[n];
                for (int i = 0; i < n; i++) {
                    parent[i] = i;
                    size[i] = 1;
                }
            }

            /* 将 p 和 q 连接 */
            public void union(int p, int q) {
                int rootP = find(p);
                int rootQ = find(q);
                if (rootP == rootQ)
                    return;

                // 小树接到大树下面，较平衡
                if (size[rootP] > size[rootQ]) {
                    parent[rootQ] = rootP;
                    size[rootP] += size[rootQ];
                } else {
                    parent[rootP] = rootQ;
                    size[rootQ] += size[rootP];
                }
                count--;
            }

            /* 判断 p 和 q 是否连通 */
            public boolean connected(int p, int q) {
                int rootP = find(p);
                int rootQ = find(q);
                return rootP == rootQ;
            }

            private int find(int x) {
                while (parent[x] != x) {
                    // 进行路径压缩
                    parent[x] = parent[parent[x]];
                    x = parent[x];
                }
                return x;
            }

            /* 返回图中有多少个连通分量 */
            public int count() {
                return count;
            }

            /* 返回图中联通集合 */
            public Map<Integer, List<Integer>> outputAggregateSet(int n) {
                Map<Integer, List<Integer>> aggregateMap = new HashMap<>();

                // 共有n个集合
                for (int i = 0; i < n; i++) {
                    int parentId = find(i);
                    if (!aggregateMap.containsKey(parentId)) aggregateMap.put(parentId, new ArrayList<>());
                    aggregateMap.get(parentId).add(i);
                }

                System.out.println(aggregateMap.toString());
                return aggregateMap;
            }
        }


        public int removeStones(int[][] stones) {
            if (stones.length <= 1) return 0;
            UF uf = new UF(stones.length);
            Map<Integer, List<Integer>> rowCacheMap = new HashMap<>();
            Map<Integer, List<Integer>> colCacheMap = new HashMap<>();
            for (int i = 0; i < stones.length; i++) {
                int row = stones[i][0], col = stones[i][1];

                if (!rowCacheMap.containsKey(row)) rowCacheMap.put(row, new ArrayList<>());
                rowCacheMap.get(row).add(i);

                if (!colCacheMap.containsKey(col)) colCacheMap.put(col, new ArrayList<>());
                colCacheMap.get(col).add(i);
            }

            for (int i = 0; i < stones.length; i++) {
                int row = stones[i][0], col = stones[i][1];

                if (rowCacheMap.containsKey(row)) {
                    for (Integer nextId : rowCacheMap.get(row)) {
                        uf.union(i, nextId);
                    }
                }

                if (colCacheMap.containsKey(col)) {
                    for (Integer nextId : colCacheMap.get(col)) {
                        uf.union(i, nextId);
                    }
                }
            }

            return stones.length - uf.count;
        }
    }

    public void run() {
        Solution solution = new Solution();
        System.out.println(solution.removeStones(InputUtil.toDoubleIntegerArray("[[0,0],[0,1],[1,0],[1,2],[2,1],[2,2]]")));
        System.out.println(solution.removeStones(InputUtil.toDoubleIntegerArray("[[0,0],[0,2],[1,1],[2,0],[2,2]]")));
        System.out.println(solution.removeStones(InputUtil.toDoubleIntegerArray("[[0,0]]")));
        System.out.println(solution.removeStones(InputUtil.toDoubleIntegerArray("[[0,1],[1,0]]")));
    }

    public static void main(String[] args) throws Exception {
        LC947 an = new LC947();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
